package model

import "project/schedule/util"

func Allocating(g *General, p *Project, allocation *Allocation, staff *Staff,
	employee, projectScale, startDate, endDate string) {
	if allocation.GroupJudge == "" && allocation.GroupLeader == "" &&
		staff.GroupJudge[projectScale] && staff.GroupLeader[projectScale] {
		before, after := util.AuditDay(p.ProposedDay, "主审", g.AuditRule)
		ifJoinStartDate := util.AllDayIndex2Value[util.AllDayValue2Index[startDate]-before]
		ifJoinEndDate := util.AllDayIndex2Value[util.AllDayValue2Index[endDate]+after]
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.WorkRegion) {
			return
		}
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.Vacation) {
			return
		}
		allocation.GroupJudge = employee
		allocation.GroupLeader = employee
		return
	}
	if allocation.GroupLeader == "" && staff.GroupLeader[projectScale] {
		before, after := util.AuditDay(p.ProposedDay, "组长", g.AuditRule)
		ifJoinStartDate := util.AllDayIndex2Value[util.AllDayValue2Index[startDate]-before]
		ifJoinEndDate := util.AllDayIndex2Value[util.AllDayValue2Index[endDate]+after]
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.WorkRegion) {
			return
		}
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.Vacation) {
			return
		}
		allocation.GroupLeader = employee
		return
	}
	if allocation.GroupJudge == "" && staff.GroupJudge[projectScale] {
		before, after := util.AuditDay(p.ProposedDay, "主审", g.AuditRule)
		ifJoinStartDate := util.AllDayIndex2Value[util.AllDayValue2Index[startDate]-before]
		ifJoinEndDate := util.AllDayIndex2Value[util.AllDayValue2Index[endDate]+after]
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.WorkRegion) {
			return
		}
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.Vacation) {
			return
		}
		allocation.GroupJudge = employee
		return
	}
	groupLeaderJudgeSlice := make([]string, 0)
	groupLeaderJudgeSlice = append(groupLeaderJudgeSlice, allocation.GroupJudge)
	groupLeaderJudgeSlice = append(groupLeaderJudgeSlice, allocation.GroupLeader)
	different := len(util.Duplicate(groupLeaderJudgeSlice))
	if staff.GroupMember[projectScale] && len(allocation.GroupMember) < p.ProposedPerson-different {
		before, after := util.AuditDay(p.ProposedDay, "组员", g.AuditRule)
		ifJoinStartDate := util.AllDayIndex2Value[util.AllDayValue2Index[startDate]-before]
		ifJoinEndDate := util.AllDayIndex2Value[util.AllDayValue2Index[endDate]+after]
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.WorkRegion) {
			return
		}
		if util.CrossOverMulti(ifJoinStartDate, ifJoinEndDate, staff.Vacation) {
			return
		}
		allocation.GroupMember = append(allocation.GroupMember, employee)
	}
}
